package cn.edu.hbwe.book.servlet.client;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import  cn.edu.hbwe.book.entity.PageBean;
import  cn.edu.hbwe.book.service.ProductService;

/**
 * Servlet implementation class MenuSearchServlet
 */
public class MenuSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MenuSearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	
	}
		public void doPost(HttpServletRequest requst, HttpServletResponse response)
				throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
	        response.setContentType("text/html;charset=UTF-8");
	        requst.setCharacterEncoding("UTF-8"); 
			// 定义当前页码，默认为1
			int currentPage = 1;
			String _currentPage = requst.getParameter("currentPage");
			if (_currentPage != null) {
				currentPage = Integer.parseInt(_currentPage);
			}
			// 定义每页显示条数,默认为4
			int currentCount = 4;	
			String _currentCount = requst.getParameter("currentCount");
			if (_currentCount != null) {
				currentCount = Integer.parseInt(_currentCount);
			}
			//获取前台页面搜索框输入的值
			String searchfield = requst.getParameter("textfield");
			//如果搜索框中没有输入值，则表单传递的为默认值，此时默认查询全部商品目录
			if("请输入书名".equals(searchfield)){
				requst.getRequestDispatcher("/showProductByPage").forward(requst, response);
				return;
			}
			//调用service层的方法，通过书名模糊查询，查找相应的图书
			ProductService service = new ProductService();
			PageBean bean = service.findBookByName(currentPage,currentCount,searchfield);
			// 将数据存储到request范围，跳转到product_search_list.jsp页面展示
			requst.setAttribute("bean", bean);
			requst.getRequestDispatcher("/client/product_search_list.jsp").forward(requst, response);
		}
	}
